package cn.tyoui.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tyoui.bean.PriceBean;
import cn.tyoui.service.PriceManageService;

@Controller
public class PriceController {

	@Resource
	private PriceManageService priceManageService ;
	
	@ResponseBody
	@RequestMapping("jsddzj")
	public Map<String,Object>  jsddzj(@RequestBody Map<String,String> map){
		Map<String,Object> resultMap = new HashMap<>();
		System.out.println(map.get("ddmx"));
		String ddmx = map.get("ddmx");
		String ddmx1 = ddmx.substring(0, ddmx.length()-1);
		System.out.println(ddmx1);
		String[] ddmxArr = ddmx1.split(",");
		System.out.println("ddmx.length : "+ddmxArr.length);
		int zj = 0;
		for(int i = 0;i < ddmxArr.length;i++) {
			String a = ddmxArr[i];
			String[] aa = a.split(";");
			System.out.println("aa.length :" + aa.length);
			int yfsl = 0;
			PriceBean priceBean = new PriceBean();
			for(int j = 0;j < aa.length;j++) {
				if(j == 0) {
					priceBean.setYfzl(aa[0]);
				}
				if(j == 1) {
					priceBean.setXyff(aa[1]);;
				}
				if(j == 2) {
					yfsl = Integer.parseInt(aa[2]);
				}
			}
			try {
				List<PriceBean> priceBean1 =priceManageService.queryOnePrice(priceBean);
				zj += Integer.parseInt(priceBean1.get(0).getPrice())* yfsl;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("zj", zj);
		System.out.println("zj  :  "  + zj);
		return resultMap;
		
	}
	
	@ResponseBody
	@RequestMapping("query_price")
	public Map<String,Object>  queryPrice(){
		System.out.println("query_price");
		Map<String,Object> resultMap = new HashMap<>();
		try {
			List<PriceBean> prices = priceManageService.queryPrice();
			resultMap.put("prices", prices);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
		
	}
	@ResponseBody
	@RequestMapping("change_price")
	public Map<String,Object>  changePrice(@RequestBody PriceBean Price){
		System.out.println("change_price");
		Map<String,Object> resultMap = new HashMap<>();
		System.out.println(Price);
		boolean isSuccess = false;
		try {
			List<PriceBean> prices = priceManageService.queryOnePrice(Price);
			if(prices.size() == 0) {
				isSuccess = priceManageService.addPrice(Price);
			}else {
				isSuccess = priceManageService.changePrice(Price);
			}
			resultMap.put("isSuccess", isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
		resultMap.put("status", "200");
		return resultMap;
		
	}
}
